<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#assign myDateType = ["LocalDateTime"]/>
<#assign myIntType = ["Integer"]/>


<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
</#if>
<#if baseResultMap>
    <!-- ${table.comment}通用查询映射结果 -->
    <resultMap id="${table.entityName}ResultMap" type="${package.Entity}.${entity}">
        <#list table.fields as field>
            <#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}"/>
            </#if>
        </#list>
<#list table.commonFields as field><#--生成公共字段 -->
    <result column="${field.name}" property="${field.propertyName}"/>
</#list>
<#list table.fields as field>
    <#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}"/>
    </#if>
</#list>
    </resultMap>
</#if>

<#if baseColumnList>

    <!-- ${table.comment}SQL列 -->
    <sql id="${table.entityName}ColumnList">
        ${table.fieldNames}
    </sql>

    <!-- ${table.comment}分页 -->
    <#-- 拿到整个表的列名字${table.fieldNames} -->
    <select id="listPage${table.entityName}" resultMap="${table.entityName}ResultMap">
        SELECT <include refid="${table.entityName}ColumnList"/>
        FROM ${table.name}
    </select>

    <!-- ${table.comment}列表 -->
    <select id="list${table.entityName}" resultMap="${table.entityName}ResultMap">
        SELECT <include refid="${table.entityName}ColumnList"/>
        FROM ${table.name}
    </select>

    <!-- ${table.comment}新增 -->
    <insert id="insert${table.entityName}" useGeneratedKeys="true" keyColumn="${table.fields[0].name}" keyProperty="${table.fields[0].propertyName}">
        INSERT INTO ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list table.fields as field>
            <#if !field.keyFlag>
            <#if myDateType?seq_contains(field.propertyType)>
            <if test="${field.propertyName}!=null"><#--日期单独判断， 因为日期不需要!=''条件 -->
                 ${field.name},
            </if>
            <#elseif myIntType?seq_contains(field.propertyType)>
            <if test="${field.propertyName}!=null"><#--int类型单独判断， 因为int对于 !=''的判断是0 -->
                ${field.name},
            </if>
            <#else>
            <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                 ${field.name},
            </if>
            </#if>
            </#if>
        </#list>
        </trim>
        <trim prefix=" VALUES (" suffix=")" suffixOverrides=",">
        <#list table.fields as field>
            <#if !field.keyFlag><#--生成普通字段 -->
            <#if myDateType?seq_contains(field.propertyType)>
            <if test="${field.propertyName}!=null">
                 ${"#"}${"{"}${field.propertyName}${"}"},
            </if>
            <#elseif myIntType?seq_contains(field.propertyType)>
            <if test="${field.propertyName}!=null"><#--int类型单独判断， 因为int对于 !=''的判断是0 -->
                ${"#"}${"{"}${field.propertyName}${"}"},
            </if>
            <#else>
            <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                 ${"#"}${"{"}${field.propertyName}${"}"},
            </if>
            </#if>
            </#if>
        </#list>
        </trim>
    </insert>

    <!-- ${table.comment}修改 -->
    <update id="update${table.entityName}ById">
        UPDATE ${table.name}
        <set>
        <#list table.fields as field>
            <#if !field.keyFlag><#--生成普通字段 -->
            <#if myDateType?seq_contains(field.propertyType)>
            <if test="${field.propertyName}!=null">
                 ${field.name}=${"#"}${"{"}${field.propertyName}${"}"},
            </if>
            <#else>
            <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                 ${field.name}=${"#"}${"{"}${field.propertyName}${"}"},
            </if>
            </#if>
            </#if>
        </#list>
        </set>
        WHERE ${table.fields[0].name}=${"#"}${"{"}${table.fields[0].propertyName}${"}"}
    </update>

    <!-- ${table.comment}详情 -->
    <select id="get${table.entityName}ById" resultMap="${table.entityName}ResultMap">
        SELECT <include refid="${table.entityName}ColumnList"/>
        FROM ${table.name}
        WHERE ${table.fields[0].name}=${"#"}${"{"}${table.fields[0].propertyName}${"}"}
    </select>


</#if>

</mapper>